﻿using Sustainalytics.Entities;
using System;
using System.Collections.Generic;

namespace Sustainalytics.DataLayer.Tests
{
    public static class NewsTestingData
    {
        public static News[] NewsLst { get; set; }

        public static NewsTag[] NewsTagLst { get; set; }

        static NewsTestingData()
        {
            CreateNewsTags();

            NewsLst = new News[120];

            CreateNews(0, 10, NewsCategory.EsgResearch, NewsRegion.Dutch, NewsStatus.Draft, DateTime.Now.AddDays(-1),
                new List<NewsTag> { NewsTagLst[0], NewsTagLst[1], NewsTagLst[2] });
            CreateNews(10, 20, NewsCategory.EsgResearch, NewsRegion.Dutch, NewsStatus.Published, DateTime.Now.AddDays(-1),
                new List<NewsTag> { NewsTagLst[0], NewsTagLst[1], NewsTagLst[2] });
            CreateNews(20, 30, NewsCategory.EsgResearch, NewsRegion.Dutch, NewsStatus.Unpublished, DateTime.Now.AddDays(-1),
                new List<NewsTag> { NewsTagLst[0], NewsTagLst[1], NewsTagLst[2] });

            CreateNews(30, 40, NewsCategory.EsgResearch, NewsRegion.Dutch, NewsStatus.Draft, DateTime.Now.AddDays(1),
                new List<NewsTag> { NewsTagLst[0], NewsTagLst[1], NewsTagLst[2] });
            CreateNews(40, 50, NewsCategory.EsgResearch, NewsRegion.Dutch, NewsStatus.Published, DateTime.Now.AddDays(1),
                new List<NewsTag> { NewsTagLst[0], NewsTagLst[1], NewsTagLst[2] });
            CreateNews(50, 60, NewsCategory.EsgResearch, NewsRegion.Dutch, NewsStatus.Unpublished, DateTime.Now.AddDays(1),
                new List<NewsTag> { NewsTagLst[0], NewsTagLst[1], NewsTagLst[2] });

            CreateNews(60, 70, NewsCategory.EsgResearch, NewsRegion.Dutch, NewsStatus.Draft, DateTime.Now.AddDays(-1),
                new List<NewsTag> { NewsTagLst[0], NewsTagLst[3], NewsTagLst[4] });
            CreateNews(70, 80, NewsCategory.EsgResearch, NewsRegion.Dutch, NewsStatus.Published, DateTime.Now.AddDays(-1),
                new List<NewsTag> { NewsTagLst[0], NewsTagLst[3], NewsTagLst[4] });
            CreateNews(80, 90, NewsCategory.EsgResearch, NewsRegion.Dutch, NewsStatus.Unpublished, DateTime.Now.AddDays(-1),
                new List<NewsTag> { NewsTagLst[0], NewsTagLst[3], NewsTagLst[4] });

            CreateNews(90, 100, NewsCategory.EsgResearch, NewsRegion.Dutch, NewsStatus.Draft, DateTime.Now.AddDays(1),
                new List<NewsTag> { NewsTagLst[2], NewsTagLst[3], NewsTagLst[4] });
            CreateNews(100, 110, NewsCategory.EsgResearch, NewsRegion.Dutch, NewsStatus.Published, DateTime.Now.AddDays(1),
                new List<NewsTag> { NewsTagLst[2], NewsTagLst[3], NewsTagLst[4] });
            CreateNews(110, 120, NewsCategory.EsgResearch, NewsRegion.Dutch, NewsStatus.Unpublished, DateTime.Now.AddDays(1),
                new List<NewsTag> { NewsTagLst[2], NewsTagLst[3], NewsTagLst[4] });
        }

        private static void CreateNews(int fromIndex, int toIndexExclusive, NewsCategory category, NewsRegion region, NewsStatus status, DateTime unpublishedDate, List<NewsTag> tags)
        {
            for (int index = fromIndex; index < toIndexExclusive; index++)
            {
                News news = new News
                {
                    Id = Guid.Parse("00000000-0000-0000-0000-" + (index + 1).ToString().PadLeft(12, '0')),
                    Title = string.Format("UT The News {0} Title", index),
                    Body = string.Format("UT This is News {0} Body", index),
                    Category = category,
                    DateCreated = DateTime.Now.AddSeconds(index),
                    Region = region,
                    Status = status,
                    Tags = tags,
                    UnpublishedDate = unpublishedDate,
                    AddedByUserId = Guid.NewGuid(),
                    AvailableFor = NewsAvailability.All
                };

                NewsLst[index] = news;
            }
        }

        private static void CreateNewsTags()
        {
            NewsTagLst = new NewsTag[]
                {
                    new NewsTag { Id = Guid.Parse("00000000-0000-0000-0000-000000000001"), Name = "News Tag 0" },
                    new NewsTag { Id = Guid.Parse("00000000-0000-0000-0000-000000000002"), Name = "News Tag 1" },
                    new NewsTag { Id = Guid.Parse("00000000-0000-0000-0000-000000000003"), Name = "News Tag 2" },
                    new NewsTag { Id = Guid.Parse("00000000-0000-0000-0000-000000000004"), Name = "News Tag 3" },
                    new NewsTag { Id = Guid.Parse("00000000-0000-0000-0000-000000000005"), Name = "News Tag 4" }
                };
        }

        /// <summary>
        /// Initialize unit test data.
        /// </summary>
        public static void InitializeRepo(INewsRepository repo)
        {
            foreach (News news in NewsTestingData.NewsLst)
            {
                repo.AddNews(news);
            }
        }

        /// <summary>
        /// Cleanup unit test data.
        /// </summary>
        public static void CleanupRepo(INewsRepository repo)
        {
            foreach (News news in NewsTestingData.NewsLst)
            {
                repo.DeleteNews(news.Id);
            }
        }
    }
}
